<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of parser
 *
 * @author hodek
 */
function loadTemplate($path = null, $name = null) {
    if ($path == null) {
        if ($name == null) {
            return "";
        } else {
            $path = getTemplatePath($name);
        }
    }
    $fload = $path;
    if (file_exists($fload)) {
        $fd = fopen($fload, 'r');
        $size = filesize($fload);
        if ($size != 0) {
            $tpl = fread($fd, filesize($fload));
        } else {
            $tpl = "";
        }
        fclose($fd);
        return $tpl;
    } else {
        dropManifest();
        InitTemplates::creaTemplateManifest();
        if (Settings::$debug) {
            die(EventMsg::$ERROR_TEMPLATE . " $fload $path $name");
        } else {
            return "";
        }
    }
}

function parse($tpl) {
    $ex = explode('${', $tpl);
    $exs = sizeof($ex);
    for ($i = 1; $i < $exs; $i++) {
        $whole = substr($ex[$i], 0, strpos($ex[$i], "}"));
        $wholeWithBracket = '${' . $whole . '}';
        $keys = explode('_', $whole);
        //VARIABLE_globalName
        if ($keys[0] == Settings::$keyWord_Variable) {
            if (isset($GLOBALS[$keys[1]])) {
                $tpl = str_replace($wholeWithBracket, $GLOBALS[$keys[1]], $tpl);
            } else {
                $tpl = str_replace($wholeWithBracket, "", $tpl);
            }
            continue;
        }
        //LANG_langRefrence
        if ($keys[0] == Settings::$keyWord_Lang) {
            $lang = getLang();
            if (!isset($lang[$keys[1]])) {
                createLog("Key not set in lang:" . $keys[1]);
            }
            $tpl = str_replace($wholeWithBracket, $lang[$keys[1]], $tpl);
            continue;
        }
        if ($keys[0] == Settings::$keyWord_Template) {
            $tpl = str_replace($wholeWithBracket, parse(loadTemplate(null, $keys[1])), $tpl);
            continue;
        }
        //TEXT_textName_1
        //TEXT_textName
        if ($keys[0] == Settings::$keyWord_Text) {
            $result = selectText($keys[1]);
            $row = mysql_fetch_assoc($result);
            if (!$row) {
                createText($keys[1], "");
                $row['data'] = Settings::$defultText;
            }
            if (isset($keys[2])) {
                $numberOfLines = true;
            } else {
                $numberOfLines = false;
            }
            $tpl = str_replace($wholeWithBracket, $row['data'] . AdminForms::editText($keys[1], $row["data"], $numberOfLines), $tpl);
            continue;
        }
        //CAT_ADD_catName
        //CAT_catname_templateName
        //CAT_catname_templateName_1
        if ($keys[0] == Settings::$keyWord_Cat) {
            if (isset($keys[1]) && $keys[1] == "ADD" && isset($keys[2])) {
                $tpl = str_replace($wholeWithBracket, AdminForms::addCat($keys[2]), $tpl);
                continue;
            }
            if (isset($keys[2])) {
                $result = selectItems($keys[1]);
                $temp = "";
                if ($result && mysql_num_rows($result) > 0) {
                    $order = null;
                    $orderCounter = null;
                    if (isset($keys[3])) {
                        $order = $keys[3];
                        $orderCounter = 0;
                    }
                    $prevItemId = getCurrentItemId();
                    while ($row2 = mysql_fetch_assoc($result)) {
                        if ($row2["visibility"] == 1 || isAdmin() || $row2["visibility"] == null) {
                            if ($orderCounter == null && $order == null) {
                                $temp .= loadCat($row2["itemid"], $keys[2], $keys[1]);                              
                            } else {
                                $orderCounter++;
                                if ($orderCounter == $order) {
                                    $temp .= loadCat($row2["itemid"], $keys[2], $keys[1]);                                 
                                    break;
                                }
                            }
                        }
                    }
                    if ($orderCounter != null && $order != null && $orderCounter != $order) {
                        $temp .= createNewCatMapping($keys[1], $keys[2]);
                    }
                    setCurrentItemId($prevItemId);
                    $tpl = str_replace($wholeWithBracket, $temp, $tpl);
                } else {
                    $temp .= createNewCatMapping($keys[1], $keys[2]);
                    $tpl = str_replace($wholeWithBracket, $temp, $tpl);
                    continue;
                }
            } else {
                $result = selectType($keys[1]);
                if ($result) {
                    $row = mysql_fetch_assoc($result);
                    $tpl = str_replace($wholeWithBracket, $row["data"], $tpl);
                } else {
                    createText(insertItemType($keys[1]));
                    $tpl = str_replace($wholeWithBracket, Settings::$defultText, $tpl);
                    continue;
                }
            }
            continue;
        }
        //ITEM_EDIT
        //ITEM_EDIT_special
        //ITEM_itemName_TEXT
        //ITEM_VISIBILITY_NUMBER
        //VARIABLE_currentItemId
        //VARIABLE_currentCat
        if ($keys[0] == Settings::$keyWord_Item) {
            $result = selectItemValue(getCurrentItemId(), $keys[1]);
            if ($result && mysql_num_rows($result) > 0) {
                $row = mysql_fetch_assoc($result);
                $temp = "";
                if (isset($row["dynName"])) {
                    adminFormAddItem($row["dynName"], $row["dynData"], getCurrentItemId());
                    $prevItemTypeName = getCurrentItemTypeName();
                    setCurrentItemTypeName($keys[1]);
                    $temp .= Dynstruc::exctractDyn($row["dynData"]);
                    setCurrentItemTypeName($prevItemTypeName);
                }
                if (isset($row["textName"])) {
                    adminFormAddItem($row["textName"], $row["textData"], getCurrentItemId());
                    $temp .= $row["textData"];
                }
                if (isset($row["dateName"])) {
                    adminFormAddItem($row["dateName"], $row["dateData"], getCurrentItemId());
                    $temp .= dbDateTowebDate($row["dateData"]);
                }
                if (isset($row["numberName"])) {
                    adminFormAddItem($row["numberName"], $row["numberData"], getCurrentItemId());
                    $temp .= $row["numberData"];
                }
                $tpl = str_replace($wholeWithBracket, $temp, $tpl);
            } else {
                $value_name = insertItemMap($keys[1], getCurrentCat(), getCurrentItemId());
                switch ($keys[2]) {
                    case Settings::$keyWord_Text:
                        createText($value_name);
                        break;
                    case Settings::$keyWord_Dyn:
                        insertDynSruc($value_name);
                        break;
                    case Settings::$keyWord_Date:
                        insertDate($value_name);
                        break;
                    case Settings::$keyWord_Number:
                        insertNumber($value_name);
                        break;
                    default:
                        createText($value_name);
                }
                adminFormAddItem($value_name, Settings::$defultText, getCurrentItemId());
                $tpl = str_replace($wholeWithBracket, Settings::$defultText, $tpl);
                continue;
            }
            continue;
        }
        if ($keys[0] == Settings::$keyWord_Msg) {
            $lang = getLang();
            if (isset($_GET["msg"])) {
                $tpl = str_replace($wholeWithBracket, $lang[$_GET["msg"]], $tpl);
            } else {
                $tpl = str_replace($wholeWithBracket, "", $tpl);
            }
            continue;
        }
        //ADMIN_neco
        if ($keys[0] == Settings::$keyWord_Admin) {
            if (isset($keys[1]) && $keys[1] == "EDIT") {
                if (isset($keys[2])) {
                    $tpl = str_replace($wholeWithBracket, AdminForms::editCat($keys[2]), $tpl);
                } else {
                    $tpl = str_replace($wholeWithBracket, AdminForms::editCat(), $tpl);
                }
                continue;
            }
        }
    }
    return $tpl;
}

function loadCat($itemId, $template, $catName) {
    $temp = "";
    setCurrentItemId($itemId);
    $prevAdminForm = getAdminFrom();
    $prevCurrentCat = getCurrentCat();
    setCurrentCat($catName);
    $temp .= parse(loadTemplate(null, $template));
    setCurrentCat($prevCurrentCat);
    setAdminFrom($prevAdminForm);
    return $temp;
}

function createNewCatMapping($currentCat, $templateName) {
    $prevCat = getCurrentCat();
    setCurrentCat($currentCat);
    $prevItemId = getCurrentItemId();
    setCurrentItemId(genNum());
    $prevAdminForm = getAdminFrom();
    $value_name = insertItemMap("VISIBILITY", getCurrentCat(), getCurrentItemId());
    insertNumber($value_name, 1);
    $temp = parse(loadTemplate(null, $templateName));
    setAdminFrom($prevAdminForm);
    setCurrentItemId($prevItemId);
    setCurrentCat($prevCat);
    return $temp;
}

?>
